﻿using System;
using System.Globalization;

namespace LastfmNet
{
    [Serializable]
    public class LastfmException : Exception
    {
        private int errorCode = 0;
        private static Type[] exceptionTypes = new Type[]
        {
            typeof(LastfmException),
            typeof(ThisErrorDoesNotExistException),

        };

        public LastfmException()
            : base()
        {

        }

        public LastfmException(string message)
            : base(message)
        {

        }

        public LastfmException(string message, Exception innerException)
            : base(message, innerException)
        {

        }

        public static LastfmException CreateException(int errorCode, Exception innerException = null)
        {
            if (errorCode < 0 || errorCode >= exceptionTypes.Length)
            {
                // throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Error code must be less than {0} and greater than -1", Lastfm.Errors.Length));
                return new LastfmException(Lastfm.Errors[errorCode], innerException);
            }

            LastfmException exception = exceptionTypes[errorCode].GetConstructor(new Type[0]).Invoke(new object[0]) as LastfmException;
            return exception;
        }

        public int ErrorCode
        {
            get
            {
                return this.errorCode;
            }
            private set
            {
                this.errorCode = value;
            }
        }
    }
}
